from rest_framework import serializers





from apps.headlines.models import Channel, Article, Comment
from apps.questions.models import Label
from apps.users.models import User


class ArticleSearchSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ['id', 'createtime', 'content', 'title']


class UserFansSerializer(serializers.ModelSerializer):
    class Meta:
        model=User
        fields=['username']


class ArticlesUserSeriazlizer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields=['id','title']


class UserChannelSerializer(serializers.ModelSerializer) :
    articles = ArticlesUserSeriazlizer(many=True)
    fans = UserFansSerializer(many=True)
    class Meta:
        model = User
        fields = ['id', 'username', 'avatar', 'articles', 'fans']



class ArticleChannelSerializer(serializers.ModelSerializer):
    user = UserChannelSerializer()
    collected_users = UserFansSerializer(many=True)

    class Meta:
        model = Article
        fields = ['id','title', 'content', 'createtime', 'user','collected_users', 'image']

class CommentSerializer(serializers.ModelSerializer):
    user = UserChannelSerializer()
    subs=serializers.StringRelatedField(many=True)
    article_id = serializers.IntegerField()
    parent_id = serializers.IntegerField(default=None),
    class Meta:
        model=Comment
        fields=['id','user','content','article','article_id','parent_id','parent','createtime','subs']


class ArticleCommentSerializer(serializers.ModelSerializer):
    user = UserChannelSerializer()
    subs = CommentSerializer(many=True)
    parent_id = serializers.IntegerField(default=None)
    article_id = serializers.IntegerField()

    class Meta:
        model = Comment
        fields = ['id', 'user', 'subs', 'content', 'createtime', 'updatetime', 'parent', 'parent_id', 'article',
                  'article_id']

class LabelModelSerializer(serializers.ModelSerializer):
    class Meta:
        model=Label
        fields=['id']


class ArticleDetailSerializer(serializers.ModelSerializer):
    user = UserChannelSerializer()
    comments = ArticleCommentSerializer(many=True)
    collected_users = UserFansSerializer(many=True)
    collected = serializers.BooleanField(default=False)
    labels = LabelModelSerializer(many=True)
    class Meta:
        model = Article
        fields = ['id', 'user', 'comments', 'title', 'content', 'image', 'createtime', 'updatetime',
                  'visits', 'thumbup', 'comment_count', 'channel', 'collected', 'labels', 'collected_users']

class ArticleChannelSerializer(serializers.ModelSerializer):
    user = UserChannelSerializer()
    collected_users = UserFansSerializer(many=True)
    collected = serializers.BooleanField(default=False)

    class Meta:
        model = Article
        fields = ['id','title', 'content', 'createtime', 'user','collected_users', 'image', 'collected']



class ChannelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Channel
        fields = ['id', 'name']